<!DOCTYPE html>
<!--
Copyright (C) 2016 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>gr-diff-group</title>

<script src="../../../bower_components/web-component-tester/browser.js"></script>
<link rel="import" href="../../../test/common-test-setup.html"/>
<script src="gr-diff-line.js"></script>
<script src="gr-diff-group.js"></script>

<script>
  suite('gr-diff-group tests', () => {
    test('delta line pairs', () => {
      let group = new GrDiffGroup(GrDiffGroup.Type.DELTA);
      const l1 = new GrDiffLine(GrDiffLine.Type.ADD);
      const l2 = new GrDiffLine(GrDiffLine.Type.ADD);
      const l3 = new GrDiffLine(GrDiffLine.Type.REMOVE);
      l1.afterNumber = 128;
      l2.afterNumber = 129;
      l3.beforeNumber = 64;
      group.addLine(l1);
      group.addLine(l2);
      group.addLine(l3);
      assert.deepEqual(group.lines, [l1, l2, l3]);
      assert.deepEqual(group.adds, [l1, l2]);
      assert.deepEqual(group.removes, [l3]);
      assert.deepEqual(group.lineRange, {
        left: {start: 64, end: 64},
        right: {start: 128, end: 129},
      });

      let pairs = group.getSideBySidePairs();
      assert.deepEqual(pairs, [
        {left: l3, right: l1},
        {left: GrDiffLine.BLANK_LINE, right: l2},
      ]);

      group = new GrDiffGroup(GrDiffGroup.Type.DELTA, [l1, l2, l3]);
      assert.deepEqual(group.lines, [l1, l2, l3]);
      assert.deepEqual(group.adds, [l1, l2]);
      assert.deepEqual(group.removes, [l3]);

      pairs = group.getSideBySidePairs();
      assert.deepEqual(pairs, [
        {left: l3, right: l1},
        {left: GrDiffLine.BLANK_LINE, right: l2},
      ]);
    });

    test('group/header line pairs', () => {
      const l1 = new GrDiffLine(GrDiffLine.Type.BOTH);
      l1.beforeNumber = 64;
      l1.afterNumber = 128;

      const l2 = new GrDiffLine(GrDiffLine.Type.BOTH);
      l2.beforeNumber = 65;
      l2.afterNumber = 129;

      const l3 = new GrDiffLine(GrDiffLine.Type.BOTH);
      l3.beforeNumber = 66;
      l3.afterNumber = 130;

      let group = new GrDiffGroup(GrDiffGroup.Type.BOTH, [l1, l2, l3]);

      assert.deepEqual(group.lines, [l1, l2, l3]);
      assert.deepEqual(group.adds, []);
      assert.deepEqual(group.removes, []);

      assert.deepEqual(group.lineRange, {
        left: {start: 64, end: 66},
        right: {start: 128, end: 130},
      });

      let pairs = group.getSideBySidePairs();
      assert.deepEqual(pairs, [
        {left: l1, right: l1},
        {left: l2, right: l2},
        {left: l3, right: l3},
      ]);

      group = new GrDiffGroup(GrDiffGroup.Type.CONTEXT_CONTROL, [l1, l2, l3]);
      assert.deepEqual(group.lines, [l1, l2, l3]);
      assert.deepEqual(group.adds, []);
      assert.deepEqual(group.removes, []);

      pairs = group.getSideBySidePairs();
      assert.deepEqual(pairs, [
        {left: l1, right: l1},
        {left: l2, right: l2},
        {left: l3, right: l3},
      ]);
    });

    test('adding delta lines to non-delta group', () => {
      const l1 = new GrDiffLine(GrDiffLine.Type.ADD);
      const l2 = new GrDiffLine(GrDiffLine.Type.REMOVE);
      const l3 = new GrDiffLine(GrDiffLine.Type.BOTH);

      let group = new GrDiffGroup(GrDiffGroup.Type.BOTH);
      assert.throws(group.addLine.bind(group, l1));
      assert.throws(group.addLine.bind(group, l2));
      assert.doesNotThrow(group.addLine.bind(group, l3));

      group = new GrDiffGroup(GrDiffGroup.Type.CONTEXT_CONTROL);
      assert.throws(group.addLine.bind(group, l1));
      assert.throws(group.addLine.bind(group, l2));
      assert.doesNotThrow(group.addLine.bind(group, l3));
    });
  });

</script>
